package com.ruoyi.system.domain;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.constant.IotConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Forest.IotDeviceInfo;
import com.ruoyi.system.domain.Forest.second.DeviceListResult;
import com.ruoyi.system.mapper.SysBuildingMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.checkerframework.checker.units.qual.A;

/**
 * 设备对象 sys_device
 * 
 * @author ruoyi
 * @date 2024-08-07
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class SysDevice extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 设备id */
    private Long deviceId;

    /** 原始设备编码 */
    @Excel(name = "原始设备编码")
    private String deviceCode;

    /** 网关id */
    private String gatewayId;

    /** 设备编码 */
    @Excel(name = "设备编码")
    private String deviceNo;

    /** 设备名称 */
    @Excel(name = "设备名称")
    private String deviceName;

    /** 设备类型 1=热表 2=智能阀 3=温控阀 */
    @Excel(name = "设备类型 1=热表 2=智能阀 3=温控阀")
    private String deviceType;

    /** 楼id */
    @Excel(name = "楼id")
    private Long buildingId;

    /** 小区id */
    @Excel(name = "小区id")
    private Long housingId;
    /** 搜索楼号 */
    private String searchBuildingNo;
    /** 搜索单元 */
    private Integer searchUnit;
    /** 搜索楼层 */
    private Integer searchFloor;
    /** 搜索门牌 */
    private Integer searchDoor;

    /**
     * 接触绑定
     */
    private String unBind;

    /** 部门id */
    @Excel(name = "部门id")
    private Long deptId;

    /** 设备安装地址 */
    @Excel(name = "设备安装地址")
    private String address;

    /** 采集周期 1=每小时 2=每三小时 3=每6小时 4=每12小时 5=每天 */
    @Excel(name = "采集周期 1=每小时 2=每三小时 3=每6小时 4=每12小时 5=每天")
    private String collectCycle;

    /** 设备状态（0正常 1停用） */
    @Excel(name = "设备状态", readConverterExp = "0=正常,1=停用")
    private String deviceStatus;

    /** 删除标志（0代表存在 2代表删除） */
    private String delFlag;

    /** 注册时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "注册时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date registerTime;

    /** 注册者 */
    @Excel(name = "注册者")
    private String registerBy;

    /** 激活时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "激活时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date activateTime;

    /** 激活者 */
    private String activateBy;

    /**
     * 报警配置Id
     */
    private Long alarmConfigId;
    /** 查询类型 0或者null 默认查询 1:关联网关 2关联热费模板  */
    private String queryConfig;

    private SysBuilding buildingInfo;

    /** 设置锁定模式 1 是开锁定，0 是关锁定*/
    private int setMode;

    /** 设置回水温度 */
    private BigDecimal setTemp;
    /**
     * 工作模式
     */
    private String workMode;

    /**
     * 设置开度
     */
    private Integer setOpening;
    /**
     * 设置温度
     */
    private BigDecimal tempIndoor;

    /**
     * 累计热量(KWH)
     */
    private Double heatQuantity;
    /**
     * 热功率(KW)
     */
    private Double heatPower;
    /**
     * 流速
     */
    private Double flowrateReal;
    /**
     * 累计流量(m^3)
     */
    private Double volume;

    /** 拼接地址 */
    private String allAddress;

    /** 热费模板id */
    private Long heatFeeId;
    /** 热费单价 */
    private BigDecimal heatPrice;
    /**
     * 价格
     */
    private BigDecimal price;
    /**
     * 套内面积
     */
    private BigDecimal area;

    /**
     * 进水温度(℃)
     */
    private BigDecimal tempIntake;
    /**
     * 回水温度(℃)
     */
    private BigDecimal tempReturn;
    /**
     * 累计工作时间, 单位：小时
     */
    private Integer hoursOperating;
    /**
     *  热表状态
     */
    private Integer state;
    /**
     * 锁模式
     */
    private Integer lockState;

    /**
     * 开关栓状态
     */
    private String lockMode;
    /**
     * 阀门开度
     */
    private Integer valveOpening;

    /** 热费总额 */
    private BigDecimal totalFee;

    /** 统计时间 */
    private Date beginDate;
    private Date endDate;
    /** 累计热量,减去初始值 */
    private BigDecimal subHeatQuantity;
    /**
     * 上报时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timeUpload;

    /**
     * 采样时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timeSample;
    /** 短地址  G5楼 3-12-12*/
    private String shortAddress;
    /**
     * 用于查询Y3和G5
     */
    private String isTest;
    public static SysDevice of(IotDeviceInfo iotDeviceInfo) {
        SysDevice device = SysDevice.builder()
                .deviceCode(iotDeviceInfo.getDeviceCode())
                .collectCycle(IotConstants.COLLECT_CYCLE_HOUR)
                .activateTime(iotDeviceInfo.getDeviceClock())
                .build();
        device.setCreateTime(new Date());
        //阀门
        if (iotDeviceInfo.getDeviceData11() == null) {
        }else{
            device.setDeviceType( IotConstants.DEVICE_TYPE_HEATING);
        }
        return device;
    }


    public static SysDevice of(DeviceListResult iotDeviceInfo){
        SysDevice device = SysDevice.builder()
                .deviceCode(iotDeviceInfo.getNodeId())
                .gatewayId(iotDeviceInfo.getGatewayId())
                .activateTime(iotDeviceInfo.getCreationTime())
                .build();
        device.setCreateTime(new Date());
        //热表
        if (iotDeviceInfo.getDeviceType().startsWith("HEAT")) {
            device.setDeviceType( IotConstants.DEVICE_TYPE_HEATING);
        }else {
            device.setDeviceType( IotConstants.DEVICE_TYPE_VALVE);
        }
        return device;
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("deviceId", getDeviceId())
            .append("deviceCode", getDeviceCode())
            .append("deviceNo", getDeviceNo())
            .append("deviceName", getDeviceName())
            .append("deviceType", getDeviceType())
            .append("buildingId", getBuildingId())
            .append("housingId", getHousingId())
            .append("deptId", getDeptId())
            .append("address", getAddress())
            .append("collectCycle", getCollectCycle())
            .append("deviceStatus", getDeviceStatus())
            .append("delFlag", getDelFlag())
            .append("createBy", getCreateBy())
            .append("createTime", getCreateTime())
            .append("registerTime", getRegisterTime())
            .append("registerBy", getRegisterBy())
            .append("activateTime", getActivateTime())
            .append("activateBy", getActivateBy())
            .append("updateBy", getUpdateBy())
            .append("updateTime", getUpdateTime())
            .toString();
    }
}
